﻿#include<iostream>
#include<string.h>//判断字符数组是否相等
#include<stdlib.h>
#include<fstream>
#include<Windows.h>
#define MAX 100
#define OK 1
#define ERROR 0
using namespace std;
typedef struct {
	char Isbn[20];
	char name[20];
	float price;
}Book;
typedef struct {
	Book* elem;
	int length;
}SqList;
int InitList(SqList& L) {//构建空链表
	L.elem = new Book[MAX];
	if (!L.elem) exit(0);
	L.length = 0;
	return OK;
}
int  GetElem(SqList L, int i, Book& t) {//查找第I本
	if (i<1 || i>L.length) return ERROR;
	t = L.elem[i - 1];
	return OK;
}
void chu() {
	cout << "**********欢迎使用图书管理系统！";
	cout << "1.展示所有图书 2.插入图书3.删除图书4.查找图书的位置5.根据位置查找图书";
	cout << "**********" << endl;
}
void BianLi(SqList& L) {//遍历,不用取地址符
	for (int i = 0; i < L.length; i++) {
		cout << "第" << i + 1 << "本图书："<<endl;
		cout << "书的名字：" << L.elem[i].name << endl;
		cout << "书的ISBN：" << L.elem[i].Isbn << endl;
		cout << "书的价格：" << L.elem[i].price << endl;
		cout << "****************************" << endl;
	}
}
int LocateElem(SqList L, Book t) {//查找
	for (int i = 0; i < L.length; i++)
	{
		if ((L.elem[i].price == t.price) && (strcmp(L.elem[i].Isbn, t.Isbn) == 0) && (strcmp(L.elem[i].name, t.name) == 0))
			return i + 1;
	}return 0;
}
int  ListInsert(SqList& L, int i, Book t) {//插入
	if ((i < 1) || (i > L.length + 1)) return ERROR;
	if (L.length == MAX) return ERROR;
	for (int j = L.length - 1; j >= i - 1; j--)
		L.elem[j + 1] = L.elem[j];
	L.elem[i - 1] = t;
	L.length++;
	return OK;
}
int ListDelete(SqList& L, int i) {//删除
	if (i < 1 || (i > L.length)) return ERROR;
	for (int j = i; j <=L.length - 1; j++)
		L.elem[j - 1] = L.elem[j];
	L.length--;
	return OK;
}
int main() {
	SqList L;
	int i = 0;
	string head_1, head_2, head_3;
	if (!InitList(L))
		cout << "可能空间不足" << endl;
	fstream file;
	file.open("book.txt");
	file >> head_1 >> head_2 >> head_3;//过滤首行
	if (!file) {
		cout << "没有找到图书源文件";
	}
	while (!file.eof()) {
		file >> L.elem[i].name >> L.elem[i].Isbn >> L.elem[i].price;
		L.length++;
		i++;
	}
	file.close();
	chu();
	do {
		int a;
		cout << "输入序号：";
		cin >> a;
		switch (a) {
		case 1:
		{
			system("cls");
			chu();
			BianLi(L); 
		}break;
		case 2:
		{
			Book t;
			int x;
			cout << "插入的位置在第几本书前？";
			cin >> x;
			cout << "输入图书的名字：";
			cin >> t.name;
			cout << "输入图书的ISBN：";
			cin >> t.Isbn;
			cout << "输入图书的价格：";
			cin >> t.price;
			system("cls");
			if (!ListInsert(L, x, t))
				cout << "书的数量已满/插入的位置错误！" << endl;
			else
			{
				chu();
				cout << "图书插入成功！" << endl;
			}
		}break;
		case 3: {
			int x;
			cout << "删除第几本书？";
			cin >> x;
			system("cls");
			if (!ListDelete(L, x))
				cout << "输入的数值错误！" << endl;
			else {
				chu();
				cout << "删除成功" << endl;
			}
		}break;
		case 4: {
			system("cls");
			chu();
			Book t;
			cout << "输入待查找书的名称：";
			cin >> t.name;
			cout << "输入待查找书的ISBN：";
			cin >> t.Isbn;
			cout << "输入书的价格：";
			cin >> t.price;
			if (LocateElem(L, t))
				cout << "待查的书在第" << LocateElem(L, t) << "本" << endl;
			else
				cout << "没有找到图书" << endl;
		}break;
		case 5: {
			system("cls");
			chu();
			Book t;
			int x;
			cout << "所查的是第几本书？";
			cin >> x;
			if (GetElem(L, x, t))
			{
				cout << "第" << x << "本书的信息为：" << endl;;
				cout << "书的名称为：" << t.name << endl;
				cout << "书的ISBN为：" << t.Isbn << endl;
				cout << "书的价格为：" << t.price << endl;
			}
			else
				cout << "未找到该本书！" << endl;
		}break;
		default: {
			cout << "序号输入错误，请重新输入";
			break;
		}
		}
	} while (1);
}